package com.xxg.jdeploy.service;

import com.xxg.jdeploy.domain.HostDeploy;
import com.xxg.jdeploy.domain.HostInfo;
import com.xxg.jdeploy.domain.JavaWebDeployInfo;
import com.xxg.jdeploy.mapper.HostDeployMapper;
import com.xxg.jdeploy.mapper.HostInfoMapper;
import com.xxg.jdeploy.mapper.JavaWebDeployMapper;
import com.xxg.jdeploy.util.Config;
import com.xxg.jdeploy.util.ShellUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;

@Service
public class JavaWebDeployService {
	
	@Autowired
	private JavaWebDeployMapper javaWebDeployMapper;
	
	@Autowired
	private HostInfoMapper hostInfoMapper;
	
	@Autowired
	private HostDeployMapper hostDeployMapper;
	
	@Value("${shell.javawebdeploy}")
	public  String shellFileFolder;

	@Value("${javawebdeploy.basepath}")
	public  String basePath;

	@Value("${javawebdeploy.jettypath}")
	public  String TomcatPath;
	
	public List<JavaWebDeployInfo> getList() {
		return javaWebDeployMapper.getList();
	}

	public JavaWebDeployInfo getDetail(String uuid) {
		return javaWebDeployMapper.getDetail(uuid);
	}

	public void insert(JavaWebDeployInfo javaWebDeployInfo) {
		String url = javaWebDeployInfo.getUrl();
		if(url.contains(","))
		{
			javaWebDeployInfo.setUrl(url.replace(",", "").trim());
		}
		javaWebDeployMapper.insert(javaWebDeployInfo);
		HostDeploy hostDeploy= new HostDeploy();
		hostDeploy.setUuid(javaWebDeployInfo.getUuid());
		hostDeploy.setPlat(javaWebDeployInfo.getplat());
		hostDeployMapper.insert(hostDeploy);
	}
	
	public String getStatus(String uuid) throws Exception {
		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());
		if(info != null) {
			String out = ShellUtil.exec(hostInfo,"sh " + shellFileFolder+"/isrunning.sh ");
			return String.valueOf(StringUtils.hasText(out) && out.contains("0 :::8080"));
		} else {
			return "false";
		}
	}

	public String deploy(String uuid) throws Exception {
		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());
		if(info != null) {
			StringBuilder sb = new StringBuilder();

			// kill进程
			String kill = ShellUtil.exec(hostInfo,"sh " + shellFileFolder+"/kill.sh ");
			sb.append(kill);
			// 打包
			String contextPath = info.getContextPath();
			contextPath = contextPath.replace("/", "");
			if(contextPath.length() == 0) {
				contextPath = "root";
			}
			String cmdArray = "sh "+shellFileFolder+"/package.sh "+info.getUuid()+" "+ info.getUrl()+ " " +TomcatPath+ " " +basePath+"/"+info.getUuid()+" "+info.getContextPath() + " " +String.valueOf(info.getType());
			sb.append(ShellUtil.exec(hostInfo,cmdArray));
			//String finalName = getFinalName(basePath+"/"+info.getUuid());
			if(!ShellUtil.exec(hostInfo,cmdArray).equals("脚本执行失败")) {
				sb.append(ShellUtil.exec(hostInfo,"sh " +shellFileFolder+"/start.sh " + TomcatPath));
			} else {
				sb.append("打包失败");
			}
			return sb.toString();
		} else {
			return uuid + "对应的项目不存在！";
		}
	}

	public String restart(String uuid) throws Exception {

		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());

		if(info != null) {
			StringBuilder sb = new StringBuilder();
			// kill进程
			sb.append(ShellUtil.exec(hostInfo,"sh " +shellFileFolder+"/kill.sh " + info.getUuid()));
			// 启动程序
			sb.append(ShellUtil.exec(hostInfo,"sh " +shellFileFolder+"/start.sh " + TomcatPath));
			return sb.toString();
		} else {
			return uuid + "对应的项目不存在！";
		}
	}

	public String stop(String uuid) throws Exception {
		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());
		if(info != null) {
			return ShellUtil.exec(hostInfo,"sh " + shellFileFolder+"/kill.sh " + info.getUuid());
		} else {
			return uuid + "对应的项目不存在！";
		}
	}

	public String showLog(String uuid) throws Exception {
		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());
		if(info != null) {
			return ShellUtil.exec(hostInfo,"sh " + shellFileFolder+"/showlog.sh " + TomcatPath);
		} else {
			return uuid + "对应的项目不存在！";
		}
	}
	
	private String getFinalName(String uuid) {
		File dir = new File(uuid + "\target");
		File[] files = dir.listFiles();
		
		String fileName = null;
		for(File file : files) {
			String name = file.getName();
			if(file.isFile() && name.endsWith(".war")) {
				fileName = name;
			}
		}
		return fileName;
	}

	public String delete(String uuid) throws Exception {
		JavaWebDeployInfo info = javaWebDeployMapper.getDetail(uuid);
		HostInfo hostInfo= hostInfoMapper.getDetail(info.getplat());
		javaWebDeployMapper.delete(uuid);
		hostDeployMapper.delete(uuid);
		if(info != null) {
			return ShellUtil.exec(hostInfo,"sh " + shellFileFolder+"/delete.sh " + info.getUuid() + " " + TomcatPath +" "+info.getContextPath()+" "+basePath+"/"+info.getUuid());
		} else {
			return uuid + "对应的项目不存在！";
		}
	}
}
